package com.fuhan.service;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.fuhan.mapper.FuHanNode1Mapper;
import com.fuhan.pojo.FuHanNode1;
import org.python.modules._csv._csv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.activation.CommandObject;
import javax.activation.DataHandler;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Chao
 * @description:
 * @Date: 2021/3/8 20:28
 */
@Service
public class AcceptData implements CommandLineRunner{
    @Autowired
    private static final Logger LOGGER = LoggerFactory.getLogger(AcceptData.class);

    @Autowired
    private FuHanNode1Mapper fuHanNode1Mapper;

    @Override
    public void run(String... args) throws Exception {
        ServerSocket ss = new ServerSocket(5683);
        LOGGER.debug("服务器启动");
        while (true) {
            try {
                final Socket s = ss.accept();
                LOGGER.debug("客户端连接");
                boolean b = true;
                InputStream is = s.getInputStream();
                while (b) {
                    byte[] bt = new byte[1024];
                    int length = is.read(bt);
                    String str = new String(bt, 0, length);
                    System.out.println("数据：" + str);
                    LOGGER.debug("接收到的数据：{}",str);
                    if (str.contains("#")){
                        String[] split = str.split("#");
                        Map<String,String> map = new HashMap<>();
                        for (String tempStr : split){
                            String[] temp = tempStr.split(":");
                            map.put(temp[0],temp[1]);
                        }
                        FuHanNode1 fuHanNode1 = new FuHanNode1(map.get("N"),map.get("B"),map.get("H"),map.get("T"));
                        if (fuHanNode1Mapper.insertNodeDate(fuHanNode1) == 1){
                            LOGGER.debug("数据存入成功={}",fuHanNode1.toString());
                        }else {
                            LOGGER.debug("数据存入失败");
                        }
                    }
                    if (str.contains("AT+RESTORE")){
                        LOGGER.debug("接收到数据：{},断开本次连接",str);
                        break;
                    }
                }
                is.close();
                s.shutdownInput();
                s.close();
                ss.close();
            } catch (Exception e) {
                LOGGER.debug("客户端断开连接");
            }
        }
    }
}
